﻿<%@ Page Language="C#" MasterPageFile="~/Admin/admin.master" AutoEventWireup="true"
    CodeFile="GrnImports.aspx.cs" Inherits="Admin_GrnImports" Title="Import PO" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

    <script language="javascript">

        function CalculatePrice(obj) {
            var quantity = parseFloat($(obj).val()); //Lay text TextBox
            var costPrice = parseFloat($(obj).closest("tr").find("td").eq(4).find("input").eq(0).val()); // Lay Exchange Rate
            var total = parseFloat(quantity * costPrice);
            var textTotal = $(obj).closest("tr").find("td").eq(9); // lay nguyen doi tuong
            textTotal.text(CommaFormatted(total, 0));
        }
        //xu ly thay  doi gia
        function CalculateChangePrice(obj) {
            var quantity = $(obj).closest("tr").find("td").eq(2).find("input").eq(0); // quantity
            CalculatePrice(quantity);
            var oldCost = parseFloat($(obj).closest("tr").find("td").eq(10).find("input:hidden").eq(1).val());
            var newCost = parseFloat($(obj).val());
            if (oldCost == newCost) {
                $(obj).next().focus();
            }
            else {
                var sellPriceNew = $(obj).closest("tr").find("td").eq(6).find("input").eq(0); // Lay SellPriceNew Rate
                //                var markupPercent = parseFloat($(obj).closest("tr").find("td").eq(9).find("span").eq(0).text()); // Lay MarkupPercent
                var markupPercent = parseFloat($(obj).closest("tr").find("td").eq(10).find("input:hidden").eq(2).val());
                var costPrice = parseFloat($(obj).val());
                var sellPrice = parseFloat(costPrice * (markupPercent + 100) / 100);
                sellPriceNew.val(CommaFormatted(sellPrice));
                $(sellPriceNew).addClass("input-warning");
            }
        }
        //Ham Xu Ly Space Trong Sell Price
        function FillPrice(sender, e) {
            var key;
            if (window.event)
                key = window.event.keyCode;
            else if (e)
                key = e.which;
            else
                return true;
            if (key == 32) {
                var oldSellPrice = $(sender).closest("tr").find("td").eq(5).find("span").text();
                $(sender).val(oldSellPrice);
                $(sender).removeClass("input-warning");
                return false;
            }
            return true;
        }
        //ham xu ly delete trên gridview
        var Rows = 1;
        function DeleteRow(obj) {
            $(obj).closest("tr").fadeOut(1000, function() {
                $(obj).closest("tr").find("td").eq(10).find("input:hidden").eq(0).val("Deleted" + Rows);
                Rows = Rows + 1;
            });
        }
        function Undo() {
            Rows = Rows - 1;
            var hf = $("#<%=GridView1.ClientID%>").find('input:hidden[value="Deleted' + Rows + '"]');
            $(hf).closest("tr").fadeIn(1000);
            $(hf).closest("tr").find("td").eq(10).find("input:hidden").eq(0).val("Delete");
        }
    </script>

    <br />
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="PODetailId"
        DataSourceID="ImportPoDetailDataSource" BorderColor="#CCCCCC" BorderStyle="None"
        BorderWidth="1px" onrowcreated="GridView1_RowCreated">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <a onclick="DeleteRow(this);" style="cursor: pointer">Delete</a>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="DrugName" HeaderText="DrugName" SortExpression="[DrugId]" />
            <asp:TemplateField HeaderText="Quantity">
                <ItemTemplate>
                    <asp:TextBox ID="TxtQuantity" runat="server" Text='<%# Bind("RemainQuantity") %>'
                        TabIndex="1" OnBlur="javascript:CalculatePrice(this);" Width="80px"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal_Quantity" runat="server" Display="Dynamic"
                        ControlToValidate="TxtQuantity" ErrorMessage="Required"></asp:RequiredFieldValidator>
                    <asp:RangeValidator ID="RangeVal_Quantity" runat="server" Display="Dynamic" ControlToValidate="TxtQuantity"
                        ErrorMessage="Over Qty" MaximumValue='<%# Bind("RemainQuantity") %>' MinimumValue="0"
                        Type="Integer"></asp:RangeValidator>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="RemainQuantity" HeaderText="PoQuantity" SortExpression="[RemainQuantity]" />
            <asp:TemplateField HeaderText="CostPrice">
                <ItemTemplate>
                    <asp:TextBox ID="TxtCostPrice" runat="server" Text='<%# Bind("CostPrice","{0:#,##0.00}") %>' Width="120px"
                        TabIndex="200" OnBlur="javascript:CalculateChangePrice(this);"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal_CostPrice" runat="server" Display="Dynamic"
                        ControlToValidate="TxtCostPrice" ErrorMessage="Required"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="SellPrice Old">
                <ItemTemplate>
                    <asp:Label ID="LblSellPrice" runat="server" Text='<%# Bind("SellPrice","{0:#,##0.00}") %>'></asp:Label>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Right" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="SellPrice New">
                <ItemTemplate>
                    <asp:TextBox ID="TxtSellPrice" runat="server" Text='<%# Bind("SellPrice","{0:#,##0.00}") %>'
                        Width="120px" OnKeyPress="javascript:return FillPrice(this,event);" TabIndex="200"
                         ></asp:TextBox>
                    <asp:RequiredFieldValidator ID="ReqVal_SellPrice" runat="server" Display="Dynamic"
                        ControlToValidate="TxtSellPrice" ErrorMessage="Required"></asp:RequiredFieldValidator>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="BatchLot">
                <ItemTemplate>
                    <asp:TextBox ID="TxtBatchLot" runat="server" Text="" Width="80px" TabIndex="200"></asp:TextBox>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Right" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ExpDate">
                <ItemTemplate>
                    <asp:TextBox ID="Txtexpdate" runat="server" Text="" CssClass="datepicker" Width="120px"
                        TabIndex="200"></asp:TextBox>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Right" />
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Total">
                <ItemTemplate>
                    <%#(Decimal.Parse(Eval("CostPrice").ToString())*Int32.Parse(Eval("Quantity").ToString())).ToString("#,##0.00") %>
                </ItemTemplate>
                <ItemStyle HorizontalAlign="Right" />
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <asp:HiddenField runat="server" ID="HFdelete" Value="delete" />
                    <asp:HiddenField runat="server" ID="HFoldCost" Value='<%#Eval("CostPrice") %>' />
                    <asp:HiddenField runat="server" ID="HFMarkupPercent" Value='<%# Bind("MarkupPercent","{0:##0}") %>' />
                    <asp:HiddenField runat="server" ID="HFDrugId" Value='<%#Eval("DrugId") %>' />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="White" ForeColor="#000066" />
        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        <RowStyle ForeColor="#000066" />
        <EmptyDataTemplate>
            <b>No GrnDetails Found!</b>
        </EmptyDataTemplate>
    </asp:GridView>
    <asp:HiddenField ID="WhereClauseHiddenField" runat="server" Value="" Visible="false" />
    <data:ImportPoDetailsDataSource ID="ImportPoDetailDataSource" runat="server" SelectMethod="GetPaged"
        EnablePaging="true" EnableSorting="true">
        <Parameters>
            <asp:ControlParameter Name="WhereClause" ControlID="WhereClauseHiddenField" PropertyName="Value" />
        </Parameters>
    </data:ImportPoDetailsDataSource>
    <br />
    <br />
    <table border="0">
        <tr>
            <td>
                <asp:Button runat="server" ID="btnImports" Text="Imports" OnClick="btnImports_Click"
                    CssClass="button"></asp:Button>
            </td>
            <td>
                <input type="button" onclick="Undo();" value="Undo Delete" class="button" />
            </td>
        </tr>
    </table>
    <br />
</asp:Content>
